package com.econ.powercloud.ems.websocket;

import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author ben
 */
@Component
@Slf4j
public class WebsocketObserverManager {

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    /**
     * 用户订阅点号缓存
     * 第一个key: websocketSessionId, 第一个key:标签scene(表示是某一块位置,前端定义), value:pid集合
     */
    private static ConcurrentHashMap<String, Map<String,Set<String>>> USERS_PID_MAP = new ConcurrentHashMap<>();

    /**
     * 根据scene集合清除
     * @param websocketSessionId
     * @param scenes
     */
    public void clearDataBySessionIdAndScenes(String websocketSessionId, List<String> scenes){
        lock.writeLock().lock();
        try {
            if(CollectionUtils.isEmpty(scenes)){
                USERS_PID_MAP.remove(websocketSessionId);
                return;
            }
            if(MapUtils.isNotEmpty(USERS_PID_MAP.get(websocketSessionId))){
                scenes.stream().forEach(d->{
                    USERS_PID_MAP.get(websocketSessionId).remove(d);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.writeLock().unlock();
        }

    }

    /**
     * 订阅设备 及点号
     * @param simpSessionId 建立websocket时的simpSessionId
     * @param scene 前端标签
     * @param pidSet 点号集合
     */
    public void subscribeDevicePid(String simpSessionId, String scene, Set<String> pidSet) {
        lock.writeLock().lock();
        try {
            if(CollectionUtils.isEmpty(pidSet)){
                return;
            }
            if(MapUtils.isEmpty(USERS_PID_MAP.get(simpSessionId))){
                Map<String,Set<String>> sceneMap = Maps.newHashMap();
                sceneMap.put(scene,pidSet);
                USERS_PID_MAP.put(simpSessionId,sceneMap);
                return;
            }
            if(CollectionUtils.isEmpty(USERS_PID_MAP.get(simpSessionId).get(scene))){
                USERS_PID_MAP.get(simpSessionId).put(scene,pidSet);
                return;
            }
            Set<String> originList = USERS_PID_MAP.get(simpSessionId).get(scene);
            originList.addAll(pidSet);
            USERS_PID_MAP.get(simpSessionId).put(scene,originList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.writeLock().unlock();
        }

    }

    /**
     * 获取用户订阅设备
     * @param websocketSessionId 建立websocket时的simpSessionId
     * @return
     */
    public Set<String> getPidSetBySessionId (String websocketSessionId) {
        if(MapUtils.isEmpty(USERS_PID_MAP.get(websocketSessionId))){
            return Sets.newHashSet();
        }
        Set<String> result = Sets.newHashSet();
        USERS_PID_MAP.get(websocketSessionId).values().stream().forEach(d->{
            result.addAll(d);
        });
        return result;
    }

}
